//
//  BigFavorableViewController.m
//  7.13苏宁框架
//
//  Created by mini on 15/7/19.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "BigFavorableViewController.h"
#import "DSZMainViewController.h"
#import "BigHeadView.h"
#import "BigNumber1Cell.h"
#import "Common.h"
#import "XQViewController.h"
#import "MJChiBaoZiHeader.h"
#import "MJExtension.h"
#import "MBProgressHUD.h"
#import "MBProgressHUD+MJ.h"

static const CGFloat MJDuration = 2.0;
@interface BigFavorableViewController ()
@end

@implementation BigFavorableViewController

-(void)viewWillAppear:(BOOL)animated
{
    //隐藏导航栏和分栏
    self.navigationController.navigationBarHidden = YES;
    DSZMainViewController *con = (DSZMainViewController*)self.tabBarController;
    [con hiddenMainTabbar:YES];
    //把表放在最下面
    [self.view sendSubviewToBack:self.tableView];
    //背景放最上面
    [self.view bringSubviewToFront:self.backgroundView];
    //把背景上的三个按钮放在背景上面
    [self.view insertSubview:self.shareView aboveSubview:self.backgroundView];
    [self.view insertSubview:self.homeVIew aboveSubview:self.backgroundView];
    [self.view insertSubview:self.refreshView aboveSubview:self.backgroundView];
   //把分栏放在表上面
    [self.view insertSubview:self.tabView aboveSubview:self.tableView];
    self.tabView.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //创建表
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, 320, 480-44) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    self.tableView = tableView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor colorWithRed:238 green:238 blue:238 alpha:1];
    [self.view addSubview:tableView];
    self.height = 170;
    [self addHeadView];
    _secondView = [BigSecondView createListView];
    _secondView.delegate = self;
    _secondView.frame = CGRectMake(0,64, 320, 35);
    _secondView.hidden = YES;
    [self.view addSubview:_secondView];

    
}

-(void)addHeadView
{
    BigHeadView *head = [BigHeadView createHead];
    self.tableView.tableHeaderView = head;
}

- (IBAction)closeClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)returnHomeClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)moreClick {
    if (self.backgroundView.hidden==YES) {
        self.backgroundView.hidden = NO;
        self.homeVIew.hidden = NO;
        self.refreshView.hidden = NO;
        self.shareView.hidden = NO;
    }
    else
    {
        self.backgroundView.hidden = YES;
        self.homeVIew.hidden = YES;
        self.refreshView.hidden = YES;
        self.shareView.hidden = YES;
    }
}
- (IBAction)shareClick {
[UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"55b8ac0367e58ea260001477"
                                          shareText:@"友盟社会化分享让您快速实现分享等社会化功能，www.umeng.com/social"
                                         shareImage:[UIImage imageNamed:@"psb-2.jpeg"]
                                    shareToSnsNames:@[UMShareToSina,UMShareToQzone,UMShareToFacebook]
                                           delegate:self];
}
- (IBAction)homeClick {
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (IBAction)refreshClick {
//    self.tableView.header = [MJChiBaoZiHeader headerWithRefreshingTarget:self refreshingAction:@selector(beginRefreshing)];
//   MBProgressHUD showSuccess:@"刷新成功"
    [MBProgressHUD showMessage:@""];
    
    [self performSelector:@selector(end) withObject:nil afterDelay:1.5];
}

-(void)end
{
    [MBProgressHUD hideHUD];
    [MBProgressHUD showSuccess:@"刷新成功"];
    self.backgroundView.hidden = YES;
    self.homeVIew.hidden = YES;
    self.refreshView.hidden = YES;
    self.shareView.hidden = YES;

}


#pragma mark 下拉刷新数据
- (void)loadNewData
{
    
    // 2.模拟2秒后刷新表格UI（真实开发中，可以移除这段gcd代码）
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableView reloadData];
        [MBProgressHUD showSuccess:@"刷新成功"];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.header endRefreshing];
    });
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cells = [[UITableViewCell alloc]init];
        
        if (indexPath.row == 0) {
            BigNumber1Cell *cell = [[BigNumber1Cell alloc]init];
            cells = cell;
        }
    if (indexPath.row==1) {
        BigNumber2Cell *cell = [[BigNumber2Cell alloc]init];
        cell.delegate = self;
        if (_height==288) {
            cell.moreView.hidden = YES;
            cell.secondView.hidden = NO;
        }
        else
        {
            cell.moreView.hidden = NO;
            cell.secondView.hidden = YES;
        }
        cells = cell;
    }
    if (indexPath.row == 2) {
        BigNumber3Cell *cell = [[BigNumber3Cell alloc]init];
        cell.delegate  =self;
        cells = cell;
    }
    cells.selectionStyle = UITableViewCellSelectionStyleNone;
     return cells;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int height = 0;
    if (indexPath.row==0) {
        height = 140;
    }
    if (indexPath.row==1) {
        height = _height;
    }
    if (indexPath.row==2) {
        height = 1910;
    }
    return height;
}

//代理方法，用于重新刷表设置高度
-(void)setHeight:(NSInteger)height
{
    _height = height;
    [self.tableView reloadData];
}

//根据第二个单元格的高度来判断当表滑动到什么地方时显示
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.height==170&&scrollView.contentOffset.y>=468) {
        _secondView.hidden = NO;
    }
    else if (self.height==288&&scrollView.contentOffset.y>=583)
    {
         _secondView.hidden = NO;
    }
    else
    {
        _secondView.hidden = YES;
    }
    //当表滚动时隐藏分栏
    self.tabView.hidden = YES;
}
//当表不滚动时现实分栏
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.tabView.hidden = NO;
}
-(void)refreshList:(CGFloat)height
{
    _heigh2 = height;
    [self.tableView reloadData];
    [self setList];
}

-(void)setList
{
    if (_heigh2==35) {
        _secondView.listView.hidden = YES;
        CGRect rect = self.secondView.frame;
        rect.size.height = 35;
        self.secondView.frame = rect;
    }
    if (_heigh2==220)
    {
        _secondView.listView.hidden = NO;
        CGRect rect = self.secondView.frame;
        rect.size.height = 220;
        self.secondView.frame = rect;
    }

}

-(void)bigPush:(XQModel *)model
{
    _model = model;
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"XQ" bundle:nil];
    XQViewController *con = (XQViewController*)[story instantiateViewControllerWithIdentifier:@"xq"];
    con.model = _model;
    [self.navigationController pushViewController:con animated:YES];
}
@end
